<?php
namespace Home\Controller;

use Common\Controller\HomeBaseController;

/**
 * 代理控制器
 */
class AgentController extends HomeBaseController
{

    public function _initialize()
    {
        //验证用户是否登录
        //  echo 'this is _initialize';
        $agent_id = $_SESSION['agent_id'];
        if ($agent_id == '') {
            $this->error('请先登录', '/index.php/Login/agent_login');
        }
    }
    /**
     * 首页
     */
    public function index()
    {

        $agent_id = $_SESSION['agent_id'];

        //我的下级
        $data['members'] = M('member')->where('parent_id=' . $agent_id)->count();
        //本月新增下级
        $time                  = strtotime(date('Y-m'));
        $data['month_members'] = M('member')->where('parent_id=' . $agent_id . ' and add_time>=' . $time)->count();
        //在线会员
        $data['online'] = 10;
        //公告
        $notice = M('article')->where('ac_id=10 and article_show=1')->order('article_sort asc')->select();

        $this->assign('data', $data);
        $this->assign('url', $url);
        $this->assign('notice', $notice);

        $this->display();
    }
    public function get_notice()
    {
        $id             = $_GET['id'];
        $data['msg']    = M('article')->where('ac_id=10 and article_show=1 and article_id=' . $id)->getField('article_content');
        $data['status'] = 1;
        $this->ajaxReturn($data);
    }
    public function repassword()
    {
        $LoginPass_1 = $_POST['LoginPass_1'];
        $LoginPass_3 = $_POST['LoginPass_3'];
        $old_pwd     = M('member')->where('member_id=' . $_SESSION['agent_id'])->getField('agent_password');
        if ($old_pwd != md5($LoginPass_1)) {
            $data['status'] = 0;
            $data['info']   = '旧密码填写错误';
        } else {
            $dd['member_password'] = md5($LoginPass_3);
            M('member')->where('member_id=' . $_SESSION['agent_id'])->save($dd);
            $data['status'] = 1;
            $data['info']   = '密码修改成功';
        }
        $this->ajaxReturn($data);
    }

    public function userlist()
    {
        $name = $_GET['name'];

        if (!empty($name)) {
            $map = " and tt_member.member_name like '%" . $name . "%'";
        }

        $count = M('member')->where('parent_id=' . $_SESSION['agent_id'] . $map)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出
        $list  = M('member')->where('parent_id=' . $_SESSION['agent_id'] . $map)->limit($Page->firstRow . ',' . $Page->listRows)->order('add_time desc')->select();

        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('curr', 'userlist');
        $this->display();
    }
    public function usertable()
    {
        $name       = $_GET['member_name'];
        $start_time = $_GET['start_time'];
        $end_time   = $_GET['end_time'];
        if (!empty($name)) {
            $map = " and tt_member.member_name like '%" . $name . "%'";
        }
        if (!empty($start_time)) {
            $start_time = strtotime($start_time);
            $map .= " and tt_dl_fanli.add_time>" . $start_time;
        }
        if (!empty($end_time)) {
            $end_time = strtotime($end_time) + 84800;
            $map .= " and tt_dl_fanli.add_time<" . $end_time;
        }
        $count = M('dl_fanli')->join('tt_member ON tt_dl_fanli.x_id=tt_member.member_id', 'LEFT')->where('tt_dl_fanli.m_id=' . $_SESSION['agent_id'] . $map)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出
        $list  = M('dl_fanli')->join('tt_member ON tt_dl_fanli.x_id = tt_member.member_id', 'LEFT')->field('tt_member.member_name,tt_dl_fanli.*')->where('tt_dl_fanli.m_id=' . $_SESSION['agent_id'] . $map)->limit($Page->firstRow . ',' . $Page->listRows)->order('tt_dl_fanli.add_time desc')->select();

        $count   = 0;
        $money   = 0.00;
        $f_money = 0.00;
        foreach ($list as $k => $v) {

            $count++;
            $money += $v['money'];
            $f_money += $v['f_money'];
        }

        $this->assign('money', $money);
        $this->assign('f_money', $f_money);
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->assign('page', $show);

        $this->assign('curr', 'usertable');
        $this->display();
    }
    public function user_salary()
    {

        $start_time = $_GET['start_time'];
        $end_time   = $_GET['end_time'];

        if (!empty($start_time)) {
            $start_time = strtotime($start_time);
            $map .= " and add_time>" . $start_time;
        }
        if (!empty($end_time)) {
            $end_time = strtotime($end_time) + 84800;
            $map .= " and add_time<" . $end_time;
        }
        $count = M('dl_salary')->where('m_id=' . $_SESSION['agent_id'] . $map)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出
        $list  = M('dl_salary')->where('m_id=' . $_SESSION['agent_id'] . $map)->limit($Page->firstRow . ',' . $Page->listRows)->order('add_time desc')->select();

        $money = 0.00;
        $fanli = 0.00;
        foreach ($list as $k => $v) {

            $money += $v['money'];
            $fanli += $v['salary'];
        }

        $this->assign('money', $money);
        $this->assign('fanli', $fanli);
        $this->assign('list', $list);
        $this->assign('page', $show);

        $this->assign('curr', 'user_salary');
        $this->display();
    }
    public function user_fenhong()
    {

        $start_time = $_GET['start_time'];
        $end_time   = $_GET['end_time'];

        if (!empty($start_time)) {
            $start_time = strtotime($start_time);
            $map .= " and add_time>" . $start_time;
        }
        if (!empty($end_time)) {
            $end_time = strtotime($end_time) + 84800;
            $map .= " and add_time<" . $end_time;
        }
        $count = M('dl_fenhong')->where('m_id=' . $_SESSION['agent_id'] . $map)->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 25); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出
        $list  = M('dl_fenhong')->where('m_id=' . $_SESSION['agent_id'] . $map)->limit($Page->firstRow . ',' . $Page->listRows)->order('add_time desc')->select();

        $money = 0.00;
        $fanli = 0.00;
        foreach ($list as $k => $v) {

            $money += $v['money'];
            $fanli += $v['fenhong'];
        }

        $this->assign('money', $money);
        $this->assign('fanli', $fanli);
        $this->assign('list', $list);
        $this->assign('page', $show);

        $this->assign('curr', 'user_fenhong');
        $this->display();
    }
    public function userlink()
    {
        $a = path_encode($_SESSION['agent_id']);
        //print_r(path_decode($a));exit;
        $domain = 'http://' . $_SERVER['SERVER_NAME'] . '/index.php/Register/reg';
        $url    = $domain . '?p=' . $a;
        $this->assign('url', $url);
        $this->assign('curr', 'userlink');
        $this->display();
    }
    public function password()
    {
        $this->assign('curr', 'password');
        $this->display();
    }
    public function logout()
    {
        session('agent_id', null);
        session('agent_name', null);
        redirect('/index.php/login/agent_login');
    }
    //站内信
    public function sms()
    {
        $count = M('account_log')->where('m_id=' . $_SESSION['agent_id'])->count(); // 查询满足要求的总记录数
        $Page  = new \Think\Page($count, 15); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show  = $Page->show(); // 分页显示输出

        $sms = M('account_log')->where('m_id=' . $_SESSION['agent_id'])->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        if ($_GET['p'] == '') {
            $_GET['p'] = 1;
        }
        $this->assign('sms', $sms);
        $this->assign('page', $show);
        $this->assign('p', $_GET['p']);
        $this->display();
    }
    public function getSmsOne()
    {
        $id          = $_GET['id'];
        $data['msg'] = M('account_log')->where(' id=' . $id . ' and m_id=' . $_SESSION['agent_id'])->getField('remark');
        $dd['read']  = 1;
        M('account_log')->where(' id=' . $id . ' and m_id=' . $_SESSION['agent_id'])->save($dd);
        $data['status'] = 1;
        $data['text']   = '<span class="conv-status">已读</span>';
        $this->ajaxReturn($data);
    }
    public function delSmsOne()
    {
        $id = $_GET['id'];
        M('account_log')->where(' id=' . $id . ' and m_id=' . $_SESSION['agent_id'])->delete();
        $data['status'] = 1;
        $data['msg']    = '删除消息完成';
        $this->ajaxReturn($data);
    }
}
